﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using AllenalaMente.State;
using Microsoft.Phone.Controls;

namespace AllenalaMente.Menu
{
    public partial class GameSelection : PhoneApplicationPage
    {
        private GameState gameState;
        private List<Image> memoryGameIcons, reflexGameIcons, mathGameIcons, logicGameIcons;
        private int[] selectedGames;

        public GameSelection()
        {
            InitializeComponent();

            gameState = GameStateManager.GetState();

            PageTitle.Text = Locale.Strings.GameSelection;

            buttonConfirm.Content = Locale.Strings.ButtonStart;
            textBlockMemory.Text = Locale.Strings.Memory;
            textBlockMath.Text = Locale.Strings.Math;
            textBlockLogic.Text = Locale.Strings.Logic;
            textBlockReflex.Text = Locale.Strings.Reflex;

            textBlockInfo.Text = Locale.Strings.GameSelectionInfo;

            // Creo liste immagini delle categorie, e una completa
            CreateIconLists();

            UnlockAllGames();

            // Inizializzo i giochi sbloccati dall'utente
            InitializeGames();

            // Recupero i giochi scelti dall'utente la partita precedente
            selectedGames = gameState.lastGameSelection;

            borderMemorySelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Memory] + 1);
            borderMathSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Math] + 1);
            borderReflexSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Reflex] + 1);
            borderLogicSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Logic] + 1);

            Animation.FadeIn(this, 500);
        }

        #region Initialize

        /// <summary>
        /// Inizializza le icone dei giochi disponibili
        /// </summary>
        private void InitializeGames()
        {
            int currentMemoryIcon = 0;
            int currentMathIcon = 0;
            int currentReflexIcon = 0;
            int currentLogicIcon = 0;

            for (int i = 0; i < gameState.GamesList.Count; i++)
            {
                GameInfo currentGame = gameState.GamesList[i];

                switch (currentGame.gameCategory)
                {
                    case GameCategories.Memory:
                        
                        if (currentGame.isActive)
                        {
                            Uri uri = new Uri("/AllenalaMente;component/" + currentGame.iconPath, UriKind.RelativeOrAbsolute);
                            memoryGameIcons[currentMemoryIcon].Source = new System.Windows.Media.Imaging.BitmapImage(uri);
                            memoryGameIcons[currentMemoryIcon].Visibility = System.Windows.Visibility.Visible;
                            memoryGameIcons[currentMemoryIcon].Clip = new RectangleGeometry() { RadiusX = 10, RadiusY = 10, Rect = new Rect(0, 0, 80, 80) };
                            memoryGameIcons[currentMemoryIcon].MouseLeftButtonDown += new MouseButtonEventHandler(MemoryIcon_MouseLeftButtonDown);
                            currentMemoryIcon++;
                        }
                        break;

                    case GameCategories.Math:

                        if (currentGame.isActive)
                        {
                            Uri uri = new Uri("/AllenalaMente;component/" + currentGame.iconPath, UriKind.RelativeOrAbsolute);
                            mathGameIcons[currentMathIcon].Source = new System.Windows.Media.Imaging.BitmapImage(uri);
                            mathGameIcons[currentMathIcon].Visibility = System.Windows.Visibility.Visible;
                            mathGameIcons[currentMathIcon].Clip = new RectangleGeometry() { RadiusX = 10, RadiusY = 10, Rect = new Rect(0, 0, 80, 80) };
                            mathGameIcons[currentMathIcon].MouseLeftButtonDown += new MouseButtonEventHandler(MathIcon_MouseLeftButtonDown);
                            currentMathIcon++;
                        }
                        break;

                    case GameCategories.Reflex:

                        if (currentGame.isActive)
                        {
                            Uri uri = new Uri("/AllenalaMente;component/" + currentGame.iconPath, UriKind.RelativeOrAbsolute);
                            reflexGameIcons[currentReflexIcon].Source = new System.Windows.Media.Imaging.BitmapImage(uri);
                            reflexGameIcons[currentReflexIcon].Visibility = System.Windows.Visibility.Visible;
                            reflexGameIcons[currentReflexIcon].Clip = new RectangleGeometry() { RadiusX = 10, RadiusY = 10, Rect = new Rect(0, 0, 80, 80) };
                            reflexGameIcons[currentReflexIcon].MouseLeftButtonDown += new MouseButtonEventHandler(ReflexIcon_MouseLeftButtonDown);
                            currentReflexIcon++;
                        }
                        break;

                    case GameCategories.Logic:

                        if (currentGame.isActive)
                        {
                            Uri uri = new Uri("/AllenalaMente;component/" + currentGame.iconPath, UriKind.RelativeOrAbsolute);
                            logicGameIcons[currentLogicIcon].Source = new System.Windows.Media.Imaging.BitmapImage(uri);
                            logicGameIcons[currentLogicIcon].Visibility = System.Windows.Visibility.Visible;
                            logicGameIcons[currentLogicIcon].Clip = new RectangleGeometry() { RadiusX = 10, RadiusY = 10, Rect = new Rect(0, 0, 80, 80) };
                            logicGameIcons[currentLogicIcon].MouseLeftButtonDown += new MouseButtonEventHandler(LogicIcon_MouseLeftButtonDown);
                            currentLogicIcon++;
                        }
                        break;
                }
            }
        }

        /// <summary>
        /// Crea le liste delle icone dei giochi
        /// </summary>
        private void CreateIconLists()
        {
            // Creo liste immagini divise per categoria
            memoryGameIcons = new List<Image>();
            memoryGameIcons.Add(imageGameMemory1);
            memoryGameIcons.Add(imageGameMemory2);
            memoryGameIcons.Add(imageGameMemory3);
            memoryGameIcons.Add(imageGameMemory4);
            memoryGameIcons.Add(imageGameMemory5);

            mathGameIcons = new List<Image>();
            mathGameIcons.Add(imageGameMath1);
            mathGameIcons.Add(imageGameMath2);
            mathGameIcons.Add(imageGameMath3);
            mathGameIcons.Add(imageGameMath4);
            mathGameIcons.Add(imageGameMath5);

            reflexGameIcons = new List<Image>();
            reflexGameIcons.Add(imageGameReflex1);
            reflexGameIcons.Add(imageGameReflex2);
            reflexGameIcons.Add(imageGameReflex3);
            reflexGameIcons.Add(imageGameReflex4);
            reflexGameIcons.Add(imageGameReflex5);

            logicGameIcons = new List<Image>();
            logicGameIcons.Add(imageGameLogic1);
            logicGameIcons.Add(imageGameLogic2);
            logicGameIcons.Add(imageGameLogic3);
            logicGameIcons.Add(imageGameLogic4);
            logicGameIcons.Add(imageGameLogic5);

            // Creo una lista unica con tutte le immagini dei giochi
            List<Image> gameIcons = new List<Image>();

            gameIcons.AddRange(memoryGameIcons);
            gameIcons.AddRange(mathGameIcons);
            gameIcons.AddRange(reflexGameIcons);
            gameIcons.AddRange(logicGameIcons);
        }

        /// <summary>
        /// Rende disponibili tutti i giochi
        /// </summary>
        private void UnlockAllGames()
        {
            for (int i = 0; i < gameState.GamesList.Count; i++)
                gameState.GamesList[i].isActive = true;
        }

        #endregion

        #region Events

        // Games Selection
        private void MemoryIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image clickedIcon = ((Image)sender);
            for (int i = 0; i < memoryGameIcons.Count; i++)
            {
                memoryGameIcons[i].Opacity = 0.7;
            }

            clickedIcon.Opacity = 1;
            selectedGames[(int)GameCategories.Memory] = memoryGameIcons.IndexOf(clickedIcon);
            borderMemorySelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Memory] + 1);
        }

        private void MathIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image clickedIcon = ((Image)sender);
            for (int i = 0; i < mathGameIcons.Count; i++)
            {
                mathGameIcons[i].Opacity = 0.7;
            }

            clickedIcon.Opacity = 1;
            selectedGames[(int)GameCategories.Math] = mathGameIcons.IndexOf(clickedIcon);
            borderMathSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Math] + 1);
        }

        private void ReflexIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image clickedIcon = ((Image)sender);
            for (int i = 0; i < reflexGameIcons.Count; i++)
            {
                reflexGameIcons[i].Opacity = 0.7;
            }

            clickedIcon.Opacity = 1;
            selectedGames[(int)GameCategories.Reflex] = reflexGameIcons.IndexOf(clickedIcon);
            borderReflexSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Reflex] + 1);
        }

        private void LogicIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image clickedIcon = ((Image)sender);
            for (int i = 0; i < logicGameIcons.Count; i++)
            {
                logicGameIcons[i].Opacity = 0.7;
            }

            clickedIcon.Opacity = 1;
            selectedGames[(int)GameCategories.Logic] = logicGameIcons.IndexOf(clickedIcon);
            borderLogicSelection.SetValue(Grid.ColumnProperty, selectedGames[(int)GameCategories.Logic] + 1);
        }

        // Confirm
        private void buttonConfirm_Click(object sender, RoutedEventArgs e)
        {
            // Calcolo il numero di giochi per tipologia per impostare l'offset in selectedGames
            int MemoryGamesCount = 0, MathGamesCount = 0, ReflexGamesCount = 0, LogicGamesCount = 0;
            for (int i = 0; i < gameState.GamesList.Count; i++)
            {
                if (gameState.GamesList[i].gameCategory == GameCategories.Memory) MemoryGamesCount++;
                if (gameState.GamesList[i].gameCategory == GameCategories.Math) MathGamesCount++;
                if (gameState.GamesList[i].gameCategory == GameCategories.Reflex) ReflexGamesCount++;
            }

            gameState.chosenGames[0] = selectedGames[0];
            gameState.chosenGames[1] = selectedGames[1] + MemoryGamesCount;
            gameState.chosenGames[2] = selectedGames[2] + MemoryGamesCount + MathGamesCount;
            gameState.chosenGames[3] = selectedGames[3] + MemoryGamesCount + MathGamesCount + ReflexGamesCount;

            // Selezione modalità Random
            for (int i = 0; i <  gameState.chosenGames.Length; i++)
            {
                if (gameState.GamesList[gameState.chosenGames[i]].gameId == GamesId.random)
                {
                    gameState.chosenGames[i] = GameStateManager.getRandomIndex(gameState.GamesList[gameState.chosenGames[i]].gameCategory);
                }
            }

            gameState.gameMode = GameMode.Normal;

            NavigationService.Navigate(new Uri("/AllenalaMente;component/Games/GamePage.xaml", UriKind.RelativeOrAbsolute));
        }

        // Bonus
        private void buttonBonus_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/BonusMenu.xaml", UriKind.RelativeOrAbsolute));
        }

        #endregion
    }
}